package Method;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.nio.charset.StandardCharsets;
import Entity.Localdata;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
public class Querydgutid {
    public Localdata execute(String dgutid) {
        Localdata localdata=null;
        java.sql.Connection conn = null;
        java.sql.Statement sqlStmt = null; //语句对象
        java.sql.ResultSet sqlRst = null; //结果集对象
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource =
                    (DataSource) context.lookup("jpa/DSTest");
            conn = dataSource.getConnection();

            sqlStmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);

//执行Sql语句
            String sqlQuery = "select * from localdata where dgutid=" + "'"+dgutid +"'";
            sqlRst = sqlStmt.executeQuery(sqlQuery);
            if (sqlRst.next()){
                localdata=new Localdata(sqlRst.getString("userid"),sqlRst.getString("password"),sqlRst.getString("localname"),
                        sqlRst.getString("phone"),dgutid,sqlRst.getString("imageurl"));
            }
        } catch (NamingException e) {

        } catch (SQLException e) {

        } finally {
            try {
                if (sqlRst != null) {
                    sqlRst.close();
                }
                if (sqlStmt != null) {
                    sqlStmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
            }
           return localdata;
        }
    }
}
